package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("goods")
public class GoodsController {

    private final SpuService spuService;

    private final SkuService skuService;



    public GoodsController(SpuService spuService,SkuService skuService){
        this.spuService = spuService;
        this.skuService = skuService;
    }


    /**
     * 关于商品spu的分页查询
     * @param page
     * @param rows
     * @param cId
     * @param bid
     * @param saleable
     * @param id
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> pageQuery(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,
            @RequestParam(value = "categoryId",required = false)Long cId,
            @RequestParam(value = "brandId",required = false)Long bid,
            @RequestParam(value = "saleable",required = false)Boolean saleable,
            @RequestParam(value = "id",required = false)Long id){



        return ResponseEntity.ok(this.spuService.pageQuery(page,rows,cId,bid,saleable,id));
    }


    /**
     * 商品新增业务
     * @param spuDTO
     * @return
     */
    @PostMapping("spu")
    public ResponseEntity<Void> addGoods(@RequestBody SpuDTO spuDTO){

        this.spuService.addGoods(spuDTO);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 商品上下架修改
     * @param id
     * @param saleable
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> modifySaleable(
            @RequestParam("id")Long id,
            @RequestParam("saleable")Boolean saleable){

        this.spuService.modifySaleable(id,saleable);
        return ResponseEntity.ok().build();
    }


    /**
     * 根据spuId查询对应的spu及spuDetail，sku信息
     * @param spuId
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id")Long spuId){

        return ResponseEntity.ok(this.spuService.querySpuById(spuId));
    }


    /**
     * 商品修改业务
     * @param spuDTO
     * @return
     */
    @PutMapping("spu")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){

        this.spuService.updateGoods(spuDTO);

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 根据sku的id集合查询对应的sku集合
     * @param ids
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuByIds(@RequestParam("ids")List<Long> ids){

        return ResponseEntity.ok(this.spuService.querySkuByIds(ids));
    }


    /**
     * 根据spuId查询对应的sku的集合
     * @param spuId
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id")Long spuId){

        return ResponseEntity.ok(this.spuService.querySkuBySpuId(spuId));
    }


    /**
     * 根据spuId查询对应的spuDetail信息
     * @param spuId
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id")Long spuId){

        return ResponseEntity.ok(this.spuService.querySpuDetailById(spuId));
    }

    /**
     * 根据spuId查询spu的所有规格参数值
     * @param id spu的id
     * @return 规格参数值
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecsValues(
            @RequestParam("id") Long id,
            @RequestParam(value = "searching", required = false) Boolean searching){
        return ResponseEntity.ok(spuService.querySpecValues(id, searching));
    }


    /**
     * 批量扣减库存，map的key为skuId，value为对应数量
     * @param minusMap
     * @return
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> deductStock(@RequestBody Map<Long,Integer> minusMap){

        this.skuService.deductStock(minusMap);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 批量加库存，map的key为skuId，value为对应数量
     * @param plusMap
     * @return
     */
    @PutMapping("/stock/plus")
    public ResponseEntity<Void> addStock(@RequestBody Map<Long,Integer> plusMap){

        this.skuService.addStock(plusMap);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


}
